package com.abel.bigwater.data.sanchuan

import com.abel.bigwater.data.BcdString
import io.netty.buffer.Unpooled
import org.joda.time.DateTime
import org.junit.Assert.assertEquals
import org.junit.Test

class FetchDataReplyTests {
    @Test
    fun testMonthBill() {
        val str = """01 20 18 06 01 12 34 56 78 90 12 34 56"""
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(str))

        val bill = FetchDataReplyMonthBill(buf.readableBytes())
        val ret = bill.parseBuffer<FetchDataReplyMonthBill>(buf)

        assertEquals(ret!!.startDate, DateTime(2018, 6, 1, 0, 0))
        assertEquals(2, ret!!.dataList!!.size)
        assertEquals(0x12345678 * 0.1, ret.dataList!![0], 1E-5)
        assertEquals(0x90123456 * 0.1, ret.dataList!![1], 1E-5)
    }

    @Test
    fun testDayBill() {
        val str = """02 20 18 08 01 01 12 34 56 78 90 12 34 56 78 90 12 34"""
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(str))

        val bill = FetchDataReplyDayBill(buf.readableBytes())
        val ret = bill.parseBuffer<FetchDataReplyDayBill>(buf)

        assertEquals(ret!!.startDate, DateTime(2018, 8, 1, 0, 0))
        assertEquals(3, ret.dataList!!.size)
        assertEquals(0x12345678 * 0.1, ret.dataList!![0], 1E-5)
        assertEquals(0x90123456 * 0.1, ret.dataList!![1], 1E-5)
        assertEquals(0x78901234 * 0.1, ret.dataList!![2], 1E-5)
    }

    @Test
    fun testWeekForward() {
        val str = """03 20 18 08 01 08 00 0F 02 01 12 34 56 78 90 12 34 56"""
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(str))

        val bill = FetchDataReplyWeekDetail(buf.readableBytes())
        val ret = bill.parseBuffer<FetchDataReplyWeekDetail>(buf)

        assertEquals(ret!!.startDate, DateTime(2018, 8, 1, 8, 0))
        assertEquals(0x0F, ret.timeInterval!!.toInt())
        assertEquals(2, ret.pointNumber!!.toInt())
        assertEquals(2, ret.forwardLongList!!.size)
        assertEquals(0x12345678, ret.forwardLongList!![0])
        assertEquals(0x90123456, ret.forwardLongList!![1])
    }

    @Test
    fun testWeekPressure() {
        val str = """05 20 18 08 01 08 00 0F 02 01 12 34 56 78"""
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(str))

        val bill = FetchDataReplyWeekDetail(buf.readableBytes())
        val ret = bill.parseBuffer<FetchDataReplyWeekDetail>(buf)

        assertEquals(ret!!.startDate, DateTime(2018, 8, 1, 8, 0))
        assertEquals(0x0F, ret.timeInterval!!.toInt())
        assertEquals(2, ret.pointNumber!!.toInt())
        assertEquals(2, ret.pressureIntList!!.size)
        assertEquals(0x1234, ret.pressureIntList!![0])
        assertEquals(0x5678, ret.pressureIntList!![1])
    }
}